package com.lanyun.auth.controller.admin.authRole;


import com.google.common.base.Preconditions;
import com.lanyun.auth.controller.admin.authRole.vo.AuthRoleRequestVO;
import com.lanyun.auth.domain.authRole.AuthRoleDomainService;
import com.lanyun.web.bean.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 角色controller
 *
 * @Author Jqcc
 * @Version 1.0
 * @Data 2025/3/12
 */
@RestController
@RequestMapping("/api-admin/role/")
@Slf4j
public class AuthRoleController {

    @Resource
    private AuthRoleDomainService authRoleDomainService;

    /**
     * 新增角色
     *
     * @param authRoleRequestVO
     * @return
     */
    @PostMapping("add")
    public Result<Boolean> add(@RequestBody AuthRoleRequestVO authRoleRequestVO) {
        try {

            Preconditions.checkNotNull(authRoleRequestVO.getRoleKey(), "角色key不能为空");
            Preconditions.checkNotNull(authRoleRequestVO.getRoleName(), "角色名称不能为空");

            return Result.success(authRoleDomainService.add(authRoleRequestVO));
        } catch (IllegalArgumentException e) {
            log.error("RoleController.add.error:{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }


    /**
     * 修改角色信息
     *
     * @param authRoleRequestVO
     * @return
     */
    @PutMapping("/update")
    public Result<Boolean> update(@RequestBody AuthRoleRequestVO authRoleRequestVO) {
        try {

            Preconditions.checkNotNull(authRoleRequestVO.getId(), "角色Id不能为空");

            return Result.success(authRoleDomainService.update(authRoleRequestVO));
        } catch (IllegalArgumentException e) {
            log.error("RoleController.update.error:{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }


    /**
     * 删除角色
     *
     * @param authRoleRequestVO
     * @return
     */
    @PostMapping("delete")
    public Result<Boolean> delete(@RequestBody AuthRoleRequestVO authRoleRequestVO) {
        try {

            Preconditions.checkNotNull(authRoleRequestVO.getId(), "角色Id不能为空");

            return Result.success(authRoleDomainService.delete(authRoleRequestVO));
        } catch (IllegalArgumentException e) {
            log.error("RoleController.delete.error:{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }
}
